/**
 * @author		Ben Layet
 * @version 	0.1
 * @date		2 Sep 2008
 */
package org.learnaholic.application.ui.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

import org.learnaholic.application.LearnaholicImpl;
import org.learnaholic.application.model.MemoryItem;
import org.learnaholic.application.model.MemoryItems;
import org.learnaholic.application.ui.LearnaholicUi;
import org.learnaholic.application.ui.UiConstants;
import org.learnaholic.ui.itemtable.LabelsTable;
import org.learnaholic.ui.itemtable.LabelsTableModel;
import org.learnaholic.ui.itemtable.LabelsTableModelEvent;
import org.learnaholic.ui.itemtable.LabelsTableModelListener;
import org.learnaholic.ui.itemtable.TableCell;

/**
 * Main frame of the application.
 */
public class LearnaholicPanel extends JPanel implements LearnaholicUi
{
	/** The selectable items */
	private final LabelsTable				labelsTable;

	/** The "Start Test" button */
	private final JButton					startTestBtn			= new JButton("Start Test");
	/** The "Start Test" button handler */
	private final StartButtonHanlder		startTestHanlder		= new StartButtonHanlder();
	/** The table event handler */
	private final LabelsTableEventHandler	labelsTableEventHandler	= new LabelsTableEventHandler();

	/**
	 * Constructor.
	 * 
	 * @param tableModel
	 */
	public LearnaholicPanel(LabelsTableModel tableModel)
	{
		setLayout(new BorderLayout());
		labelsTable = new LabelsTable(tableModel);
		setBounds(100, 100, 1200, 800);
		add(labelsTable, BorderLayout.CENTER);
		labelsTable.addModelListener(labelsTableEventHandler);
		startTestBtn.addActionListener(startTestHanlder);
		add(startTestBtn, BorderLayout.SOUTH);
		startTestBtn.setEnabled(false);
		startTestBtn.setFont(UiConstants.big_font);
	}

	/**
	 * @param blurred
	 */
	public void setBlurred(boolean blurred)
	{
		String blurredStr = blurred ? " *" : "";
		labelsTable.setAllSelected(false);
	}

	public void replaceItems(MemoryItems items)
	{
		labelsTable.clear();
		addItems(items);
	}

	public void addItems(MemoryItems items)
	{
		Set<TableCell> cells = new LinkedHashSet<TableCell>();

		for (Iterator<MemoryItem> iterator = items.iterator(); iterator.hasNext();)
		{
			MemoryItem item = iterator.next();
			cells.add(new ItemCell(item));
		}

		labelsTable.addCells(cells);
	}

	public void removeItems(MemoryItems items)
	{
		for (Iterator<MemoryItem> iterator = items.iterator(); iterator.hasNext();)
		{
			labelsTable.removeItem(iterator.next().getListItem().getProperty("kanji").getValue());
		}
	}

	/**
	 * Handles the clicks on the "Start Test" button.
	 */
	private class StartButtonHanlder implements ActionListener
	{

		/**
		 * Invoked when an action occurs.
		 */
		public void actionPerformed(ActionEvent e)
		{
			if (labelsTable.getSelected().size() > 0)
			{
				LearnaholicImpl.getInstance().startTest(labelsTable.getSelected());
			}
		}
	}

	private class LabelsTableEventHandler implements LabelsTableModelListener
	{
		/**
		 * @param evt
		 */
		public void modelChanged(LabelsTableModelEvent evt)
		{
			startTestBtn.setEnabled((labelsTable.getSelected().size() > 0));
		}
	}

}
